$(function(){
	
	var service = new EbeimService();
	var device = "";
	
    var jform = $("#j-form"),
        jsubmit = $("#j-submit");

    var tpl = (function(){
        var tpl = {};
        $("script[type='text/ysmart']").each(function(){
            var t = $(this);
            tpl[t.attr("data-name")] = t.html();
        });
        return tpl;
    })();
    
    service.getComputerMessage({},function(json){
		if(json.code == 200) {
			device = json.bindDevice;
		}else{
			plugin.msgtip({type: "i",msg: json.desc});
        }
	});

    //占位符初始化
    plugin.placeHolder({
        con: $(".j-placeHolder")
    });

    //表单验证初始化
    jform.find(".j-formcheck").bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });
    /*jform.find(".j-formcheck").bind("blur", function() {
        var t = $(this);
        var name = t.attr("data-name");
        formCheck(jform,name);
    }).bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });*/

    //表单提交
    jform.bind("submit", function(){
        if (formCheck(jform)) {
            jsubmit.attr("disabled", "disabled");
            service.login(plugin.formToJson(jform.serialize()),function(data){
            	var json = eval("(" + data + ")");
            	if(json.code == "200"){
            		document.cookie = "username="+json.user.username;
            		document.cookie = "userId="+json.user.id;
            		document.cookie = "role="+json.user.role;
        			if(json.user.isUpdateDefaultPwd == "0"){
        				window.location.href = "../html/noIntercept/reset.html?userId="+json.user.id + "&username=" + json.user.username+"&phone="+json.user.mobile+ "&isFirstLogin=" + json.user.isFirstLogin+"&labType="+json.user.labType;
        			}else{
        				if(json.user.isFirstLogin == "0" && json.user.labType != "0"){
        					if(json.user.labType == "3" || json.user.labType == "4"){
        						confirm("尊敬的客户您好，您的账号安全等级为指定设备登录。一旦绑定指定设备后，只能在绑定设备中进行数据保险箱的管理和操作。是否需要绑定您当前设备？","确定",function(){
        							window.location.href = "../html/noIntercept/unBinding.html?labType="+json.user.labType + "&username=" + json.user.username+"&phone="+json.user.mobile;
        						},function(){window.location.href = "../html/noIntercept/binding.html?labType="+json.user.labType + "&username=" + json.user.username});
        					}else{
        						window.location.href = "../html/noIntercept/unBinding.html?labType="+json.user.labType + "&username=" + json.user.username+"&phone="+json.user.mobile;
        					}
        				}else{
        					if(device != json.user.bindDevice && json.user.bindDevice != null &&json.user.bindDevice!=""){
        						window.location.href = "../html/noIntercept/noRelation.html";
        					}else{
        						service.noAuthorization({username:json.user.username},function(Data){
        							if(Data.code == 200){
        								plugin.msgtip({type: "o", msg: json.message});
		        						setTimeout(function(){
		        							if(json.overCapacitystatus != ""){
		            							confirm(json.overCapacitydesc,"空间扩容",function(){
		            								if(json.overAutuTimestatus != ""){
		            									confirm(json.overAutuTimedesc,"产品续费",function(){
		            										window.location.href = "../html/index.html";
		            									},function(){plugin.msgtip({type: "i", msg: "暂未开通续费功能！"});});
		            								}else{
		            									window.location.href = "../html/index.html";
		            								}
		            							},function(){plugin.msgtip({type: "i", msg: "暂未开通扩容功能！"});});
		            						}else if(json.overAutuTimestatus != ""){
		            							confirm(json.overAutuTimedesc,"产品续费",function(){
		    										window.location.href = "../html/index.html";
		    									},function(){plugin.msgtip({type: "i", msg: "暂未开通续费功能！"});});
		            						}else{
		            							window.location.href = "../html/index.html";
		            						}
		                                }, 1500);
        							}else{
        								jsubmit.removeAttr("disabled");
        							}
        						})
        						
        					}
        				}
        			}
        		}else{
        			jsubmit.removeAttr("disabled");
        			plugin.msgtip({type: "i",msg: json.message});
        		}
            },function(){
            	jsubmit.removeAttr("disabled");
            });
        }
        return false;
    });

})

var confirm = function(msg,str,callback1,callback2){
	plugin.confirm({
	    msg: msg,
	    btnTxt1: str,
	    onHide: function () {
	        // 隐藏弹窗时要执行的回调函数
	    	callback1() || null ;
	    },
	    yes: function () {
	    	callback2() || null ;
	    }
	})
}

// 表单验证
var formCheck = function(form,name) {

    // 显示错误信息
    var showError = function(obj, msg) {
        var error = obj.siblings('.error');
        error.show();
    };

    // 隐藏错误信息
    var hideError = function(obj) {
        var error = obj.siblings('.error');
        error.hide();
    };

    // 数据验证对象
    var dataCheck = {
        username: {
            obj: form.find("[data-name='username']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj);
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        },
        password: {
            obj: form.find("[data-name='password']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj);
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}
